package commands

import (
	"io"
	"net/mail"
	"os"

	cli "github.com/urfave/cli/v3"
	"google.golang.org/grpc/grpclog"
)

const (
	version = "v0.0.1"
	author  = "bxw430"
	email   = "916589293@qq.com"
)

var (
	// 容器运行时服务地址
	containerdAddress = "/run/containerd/containerd.sock"
)

func init() {
	// 客户端与 containerd 之间采用 gRPC 的方式通信
	// 忽略 gRPC 日志，减少日志噪音，提高 RUC 的清晰度和性能。
	grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))
}

func NewCommand() *cli.Command {
	cmd := &cli.Command{
		Name:        "ruc",
		Usage:       "Manage containers and images",
		UsageText:   "ruc [options] [command]",
		Description: `RUC(Run Ur Container) is a container management client developed on OpenCloudOS, which interacts with the containerd daemon. It provides sub-commands such as run, start, stop, rm, etc. to create, start, stop, and delete containers.`,

		Version: version,
		Authors: []any{
			&mail.Address{
				Name:    author,
				Address: email,
			},
		},

		Commands: []*cli.Command{
			createCommand,
			runCommand,
			startCommand,
			pauseCommand,
			resumeCommand,
			stopCommand,
			removeCommand,
			pruneCommand,
			listCommand,
		},

		EnableShellCompletion: true,

		ErrWriter:       os.Stderr,
		CommandNotFound: commandNotFoundHandler,
	}

	return cmd
}
